﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PQ.Util
{
    class DataValue
    {
        public double[][] data;
        public DateTime time;
        private byte[] AD_Data;
        private Configuration cfg;
        public DataValue(byte[] AD_Data, Configuration cfg)
        {
            this.AD_Data = AD_Data;
            this.cfg = cfg;
        }
        public DataValue(double[][] data, Configuration cfg)
        {
            this.data = data;
            this.cfg = cfg;
        }
        public void restoreValue()
        {
            if (data != null)
            {
                return;
            }
            int pointNum = AD_Data.Length / 8 / cfg.ch_sel;
            data = new double[12][];
            for (int i = 0; i < 12; i++)
            {
                data[i] = new double[pointNum];
            }
            switch (cfg.ch_sel)
            {
                //四通道
                case 1:
                    for (int i = 0; i < pointNum; i++)
                    {
                        data[0][i] = AD_Data[i * 8] + AD_Data[i * 8 + 1] * 256;
                        transform(ref data[0][i]);

                        data[1][i] = AD_Data[i * 8 + 4] + AD_Data[i * 8 + 5] * 256;
                        transform(ref data[1][i]);

                        data[6][i] = AD_Data[i * 8 + 2] + AD_Data[i * 8 + 3] * 256;
                        transform(ref data[6][i]);

                        data[7][i] = AD_Data[i * 8 + 6] + AD_Data[i * 8 + 7] * 256;
                        transform(ref data[7][i]);

                    }
                    break;
                //八通道
                case 2:
                    for (int i = 0; i < pointNum; i++)
                    {
                        data[0][i] = AD_Data[i * 16] + AD_Data[i * 16 + 1] * 256;
                        transform(ref data[0][i]);
                        data[1][i] = AD_Data[i * 16 + 4] + AD_Data[i * 16 + 5] * 256;
                        transform(ref data[1][i]);
                        data[2][i] = AD_Data[i * 16 + 8] + AD_Data[i * 16 + 9] * 256;
                        transform(ref data[2][i]);
                        data[3][i] = AD_Data[i * 16 + 12] + AD_Data[i * 16 + 13] * 256;
                        transform(ref data[3][i]);

                        data[6][i] = AD_Data[i * 16 + 2] + AD_Data[i * 16 + 3] * 256;
                        transform(ref data[6][i]);
                        data[7][i] = AD_Data[i * 16 + 6] + AD_Data[i * 16 + 7] * 256;
                        transform(ref data[7][i]);
                        data[8][i] = AD_Data[i * 16 + 10] + AD_Data[i * 16 + 11] * 256;
                        transform(ref data[8][i]);
                        data[9][i] = AD_Data[i * 16 + 14] + AD_Data[i * 16 + 15] * 256;
                        transform(ref data[9][i]);
                    }
                    break;
                //十二通道
                case 3:
                    for (int i = 0; i < pointNum; i++)
                    {
                        data[0][i] = AD_Data[i * 24] + AD_Data[i * 24 + 1] * 256;
                        transform(ref data[0][i]);
                        data[1][i] = AD_Data[i * 24 + 4] + AD_Data[i * 24 + 5] * 256;
                        transform(ref data[1][i]);
                        data[2][i] = AD_Data[i * 24 + 8] + AD_Data[i * 24 + 9] * 256;
                        transform(ref data[2][i]);
                        data[3][i] = AD_Data[i * 24 + 12] + AD_Data[i * 24 + 13] * 256;
                        transform(ref data[3][i]);
                        data[4][i] = AD_Data[i * 24 + 16] + AD_Data[i * 24 + 17] * 256;
                        transform(ref data[4][i]);
                        data[5][i] = AD_Data[i * 24 + 20] + AD_Data[i * 24 + 21] * 256;
                        transform(ref data[5][i]);

                        data[6][i] = AD_Data[i * 24 + 2] + AD_Data[i * 24 + 3] * 256;
                        transform(ref data[6][i]);
                        data[7][i] = AD_Data[i * 24 + 6] + AD_Data[i * 24 + 7] * 256;
                        transform(ref data[7][i]);
                        data[8][i] = AD_Data[i * 24 + 10] + AD_Data[i * 24 + 11] * 256;
                        transform(ref data[8][i]);
                        data[9][i] = AD_Data[i * 24 + 14] + AD_Data[i * 24 + 15] * 256;
                        transform(ref data[9][i]);
                        data[10][i] = AD_Data[i * 24 + 18] + AD_Data[i * 24 + 19] * 256;
                        transform(ref data[10][i]);
                        data[11][i] = AD_Data[i * 24 + 22] + AD_Data[i * 24 + 23] * 256;
                        transform(ref data[11][i]);
                    }
                    break;
           
         
        }
            //恢复真实值
            restorSingleValue(data[cfg.indexUA], cfg.Fua);
            restorSingleValue(data[cfg.indexUB], cfg.Fub);
            restorSingleValue(data[cfg.indexUC], cfg.Fuc);
            restorSingleValue(data[cfg.indexUN], cfg.Fun);
            restorSingleValue(data[cfg.indexUZ], cfg.Fuz);

            restorSingleValue(data[cfg.indexIA], cfg.Fia);
            restorSingleValue(data[cfg.indexIB], cfg.Fib);
            restorSingleValue(data[cfg.indexIC], cfg.Fic);
            restorSingleValue(data[cfg.indexIN], cfg.Fin);
        }

        //将整数转换成实际值
        private void transform(ref double data)
        {
            if (data >= 32768)
            {
                data = (data - 32768) * cfg.range / 32767 - cfg.range;
            }
            else
            {
                data = data * cfg.range / 32767;
            }
        }
 
        public void restorSingleValue(double[] d, double factor)
        {
            for (int i = 0; i < d.Length; i++)
            {
                d[i] *= factor;
            }
        }
    }
}
